from launch import LaunchDescription
from launch_ros.actions import Node
# 封装终端指令相关类--------------
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
# 参数声明与获取-----------------
# from launch.actions import DeclareLaunchArgument
# from launch.substitutions import LaunchConfiguration
# 文件包含相关-------------------
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
# 分组相关----------------------
# from launch_ros.actions import PushRosNamespace
# from launch.actions import GroupAction
# 事件相关----------------------
from launch.event_handlers import OnProcessStart, OnProcessExit
from launch.actions import ExecuteProcess, RegisterEventHandler,LogInfo
# 获取功能包下share目录路径-------
from ament_index_python.packages import get_package_share_directory
import os
# 延时
from launch.actions import TimerAction
""" 
    需求：包含其他launch文件，launch嵌套。

 """
def generate_launch_description():

    #ros2 launch ardupilot_ros apm_iris_maze.launch.py rviz:=false
    apm_iris_maze = IncludeLaunchDescription(
        launch_description_source=PythonLaunchDescriptionSource(
            launch_file_path=os.path.join(
                get_package_share_directory("ardupilot_ros"),
                "launch",
                "apm_iris_maze.launch.py"
                )
            )
    )
    #Mavproxy
    Mavproxy = ExecuteProcess(
        cmd=[
                'mavproxy.py', 
                '--out', '127.0.0.1:14550',
                '--out', '127.0.0.1:14551',
                '--master', 'tcp:127.0.0.1:5760',
                '--sitl', '127.0.0.1:5501',
                '--non-interactive'
            ],
            output='screen',  # 允许在终端输出
    )
    # 注册事件处理器：当 mode_service 成功退出时启动 arm_service
    apm_iris_maze_Mavproxy = RegisterEventHandler(
        OnProcessExit(
            target_action=apm_iris_maze,  # 监听 mode_service 节点
            # target_action=arm_service,  # 监听 mode_service 节点
            on_start=[
                # 只有在 mode_service 正常退出(exit)时，才启动 arm_service
                Mavproxy
                # mode_service
            ]
        )
    )
    return LaunchDescription([apm_iris_maze,apm_iris_maze_Mavproxy])
